package com.bluemoon.bmwalk.common.jacoco;

import org.jacoco.cli.internal.core.tools.ExecFileLoader;

import java.io.File;
import java.io.IOException;
import java.util.List;
/**
 * @program: jacoco合并文件.
 * @description: jacoco合并文件.
 * @author: liaozhioyng
 * @create: 2023-09-13
 */
public class CommanMerge {

	/**
	 * 合并文件 
	 * @param execfiles 文件集
	 * @param destfile 合并后的文件
	 */
	public static int executeMerge(List<File> execfiles, File destfile){
		int flag = 1;
		try {
			final ExecFileLoader loader = new ExecFileLoader();
			if (execfiles.isEmpty()) {
				System.out.println("[WARN] No execution data files provided.");
			} else {
				for (final File file : execfiles) {
					if (!file.exists()) { // 检查文件是否存在
						System.out.printf("[ERROR] Execution data file %s does not exist, skipped.%n",
								file.getAbsolutePath()); // 打印错误信息
						continue; // 跳过当前循环
					}
					System.out.printf("[INFO] Loading execution data file %s.%n",
							file.getAbsolutePath());
					// 开始加载exec文件，遍历多个文件加载
					loader.load(file);
				}
			}
			loader.save(destfile, true);
			flag = 0;
		} catch (IOException e) {
			e.printStackTrace();
			flag = 1;
		}
		return flag;
	}
	
}
